package per.zqx.publish_subcribe;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import per.zqx.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Subscriber01
 *
 * @author Sa  2021-03-17 13:51
 */
public class Subscriber01 {
    public static void main(String[] args) throws IOException, TimeoutException {
        String EXCHANGE_NAME = "log_fanout_exchange";
        String QUEUE_NAME = "fanout_queue";
        Channel channel = RabbitUtils.getConnection().createChannel();
        // 声明一个交换机，类型是分发（fanout）
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        // 拿到一个临时的队列
        String queueName = channel.queueDeclare().getQueue();
        // 信道绑定队列和交换机
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody(), "utf-8");
            System.out.println("Subscriber01 received a massage: " + msg);
        };
        // 消费
        channel.basicConsume(queueName, true, deliverCallback, (consumerTag) -> {
        });
    }
}
